package com.ms.game.sendgift.db;

import java.util.ArrayList;
import java.util.Calendar;

import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.sendgift.SendGiftProcessor;
import com.ms.game.sendgift.SendGiftStage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.player.Player;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;

/**
 * callback
 * @see SendGiftProcessor#OnUpdateMoneyNotify(GUID, GameLobbyModule)
 */
public class LoadAllNotifyDCLAsync extends CentralAsyncCommand {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1274747295267282551L;

	public String playerIP;
	
	public GUID playerID;
	
	public GUID sessionID;
	
	/**
	 * 屬於自己的超時資料
	 */
	private ArrayList<SendGiftNotifyModel> timeOutData = new ArrayList<SendGiftNotifyModel>();
	
	/**
	 * 過期的寄送禮物訂單內的道具資料
	 */
	private ArrayList<PlayerItemModel> timeOutGiftItems = new ArrayList<PlayerItemModel>();
	
	@Override
	public void asyncExecute() throws Exception {
		
		ArrayList<SendGiftNotifyModel> saveNotifyList= new ArrayList<SendGiftNotifyModel>();
		ArrayList<SendGiftRecordLog> logList = new ArrayList<SendGiftRecordLog>();
		
		Player player = PlayerDAO.loadPlayerByGuid(playerID);
//		Date date = 
		ArrayList<SendGiftNotifyModel> notifyList = SendGiftDAO.loadUpdateMoneyByPlayerID(playerID);
		for (SendGiftNotifyModel model : notifyList) {
			
			if(model.isTaken()){
				//領取過的就不管了
				continue;
			}
			
			if(model.getSendBackTime() != null){
				//已經退回過的
				continue;
			}
			
			//是否過期 ?
			boolean isOverTime = TimeSys.currentTimeMills() > model.getOverTime().getTime();
			
			if(player.isDisableSendGift()){
				if(!model.isFinish() || model.getStage() == SendGiftStage.FINIAL_SEND){
					//只有還在進行中的階段 跟 收方收取階段 狀態會被改成 禁止送禮退回狀態
					model.setFinish(true);
					model.setStage(SendGiftStage.DISABLE_SEND_BY_GMTOOL);
					model.setSendBackTime(Calendar.getInstance().getTime());
					saveNotifyList.add(model);
					model.setOrderFinishTime(Calendar.getInstance().getTime());
				}else {
					continue;
				}
				
			} else if (model.getStage() == SendGiftStage.FORCE_SEND_GIFT_RETURN_BY_GMTOOL) {
				if(model.getSendBackTime() != null){
					//已經退回過的
					continue;
				}
				// 單項禮物退回
				model.setFinish(true);
				model.setSendBackTime(Calendar.getInstance().getTime());
				model.setOrderFinishTime(Calendar.getInstance().getTime());
				saveNotifyList.add(model);
			} else if(!model.isFinish() && isOverTime){
				//尚未結束但是過期
				model.setFinish(true);
				model.setStage(SendGiftStage.TIME_OUT_ORDER_RETURN);
				model.setSendBackTime(Calendar.getInstance().getTime());
				model.setOrderFinishTime(Calendar.getInstance().getTime());
				saveNotifyList.add(model);
			}else {
				continue;
			}

			SendGiftRecordLog logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			logModel.setCompleteTime(model.getFinishTime());
			logModel.setSendbackTime(model.getSendBackTime());
			logModel.setOrderFinishTime(model.getOrderFinishTime());
			logModel.setSuccess(false);
			logModel.setFinish(model.isFinish());
			logModel.setTaken(false);
			logList.add(logModel);
			
		}

		//是否有要更新的資料
		if(saveNotifyList.size() > 0){
			SendGiftDAO.saveOrUpdateListSendGiftNotify(saveNotifyList);
		}
		
		//不是自己的log要先寫
		if(logList.size() > 0){
			SendGiftRecordDAO.saveOrUpdateListSendGiftLog(logList);
		}

	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
		GameLobbyModule module = (GameLobbyModule)userData[0];
		Player playerData = (Player)userData[1];
		SendGiftResponse response = (SendGiftResponse)userData[2];
		SendGiftProcessor.OnUpdateMoneyCallBack(sessionID, playerID, playerData, playerIP, module, response,
				timeOutGiftItems);

	}
	
	public ArrayList<SendGiftNotifyModel> getTimeOutData() {
		return timeOutData;
	}

	public ArrayList<PlayerItemModel> getTimeOutGiftItems() {
		return timeOutGiftItems;
	}

	
}
